﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

namespace KetBanBonPhuong
{
    /// <summary>
    /// Summary description for User
    /// </summary>
    public class User
    {
        private int _UserID;

        public int UserID
        {
            get
            {
                return _UserID;
            }
            set
            {
                _UserID = value;
            }
        }
        private String _UserName;

        public String UserName
        {
            get
            {
                return _UserName;
            }
            set
            {
                _UserName = value;
            }
        }
        private String _Password;

        public String Password
        {
            get
            {
                return _Password;
            }
            set
            {
                _Password = value;
            }
        }
        private String _FirstName;

        public String FirstName
        {
            get
            {
                return _FirstName;
            }
            set
            {
                _FirstName = value;
            }
        }
        private String _LastName;

        public String LastName
        {
            get
            {
                return _LastName;
            }
            set
            {
                _LastName = value;
            }
        }
        private String _Email;

        public String Email
        {
            get
            {
                return _Email;
            }
            set
            {
                _Email = value;
            }
        }
        private String _Avatar;

        public String Avatar
        {
            get
            {
                return _Avatar;
            }
            set
            {
                _Avatar = value;
            }
        }
        private Byte _Active;

        public Byte Active
        {
            get
            {
                return _Active;
            }
            set
            {
                _Active = value;
            }
        }
        private bool _Online;

        public bool Online
        {
            get
            {
                return _Online;
            }
            set
            {
                _Online = value;
            }
        }
        private DateTime _CreateDate;

        public DateTime CreateDate
        {
            get
            {
                return _CreateDate;
            }
            set
            {
                _CreateDate = value;
            }
        }

        public User()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        public User(SqlDataReader SqlReader)
        {
            if (SqlReader != null && !SqlReader.IsClosed)
            {
                _UserID = SqlReader.GetInt32(0);
                if (!SqlReader.IsDBNull(1))
                {
                    _UserName = SqlReader.GetString(1);
                }
                if (!SqlReader.IsDBNull(2))
                {
                    _Password = SqlReader.GetString(2);
                }
                if (!SqlReader.IsDBNull(3))
                {
                    _FirstName = SqlReader.GetString(3);
                }
                if (!SqlReader.IsDBNull(4))
                {
                    _LastName = SqlReader.GetString(4);
                }
                if (!SqlReader.IsDBNull(5))
                {
                    _Email = SqlReader.GetString(5);
                }
                if (!SqlReader.IsDBNull(6))
                {
                    _Avatar = SqlReader.GetString(6);
                }
                if (!SqlReader.IsDBNull(7))
                {
                    _Active = SqlReader.GetByte(7);
                }
                if (!SqlReader.IsDBNull(8))
                {
                    _Online = SqlReader.GetBoolean(8);
                }
                if (!SqlReader.IsDBNull(9))
                {
                    _CreateDate = SqlReader.GetDateTime(9);
                }
            }
        }

        public static DataTable GetAll()
        {
            DBHelp Dbh = new DBHelp();
            DataTable dt = new DataTable();
            String cmd = "User_GetAll";
            dt = Dbh.ExecuteDataTable(cmd, CommandType.StoredProcedure);
            return dt;
        }

        public static User GetInfo(int UserID)
        {
            DBHelp Dbh = new DBHelp();
            Dbh.AddParameter("@user_id", UserID);
            User user = new User();
            string cmd = "User_GetInfo";
            SqlDataReader SqlReader = Dbh.ExecuteReader(cmd, CommandType.StoredProcedure);
            if (SqlReader.Read())
            {
                user.UserID = SqlReader.GetInt32(0);
                if (!SqlReader.IsDBNull(1))
                {
                    user.UserName = SqlReader.GetString(1);
                }
                if (!SqlReader.IsDBNull(2))
                {
                    user.Password = SqlReader.GetString(2);
                }
                if (!SqlReader.IsDBNull(3))
                {
                    user.FirstName = SqlReader.GetString(3);
                }
                if (!SqlReader.IsDBNull(4))
                {
                    user.LastName = SqlReader.GetString(4);
                }
                if (!SqlReader.IsDBNull(5))
                {
                    user.Email = SqlReader.GetString(5);
                }
                if (!SqlReader.IsDBNull(6))
                {
                    user.Avatar = SqlReader.GetString(6);
                }
                if (!SqlReader.IsDBNull(7))
                {
                    user.Active = SqlReader.GetByte(7);
                }
                if (!SqlReader.IsDBNull(8))
                {
                    user.Online = SqlReader.GetBoolean(8);
                }
                if (!SqlReader.IsDBNull(9))
                {
                    user.CreateDate = SqlReader.GetDateTime(9);
                }
                return user;
            }
            else
            {
                return null;
            }

        }

        /// <summary>
        /// Check UserName is exist
        /// </summary>
        /// <param name="UserName"></param>
        /// <returns></returns>
        public static int GetUserByUserName(String UserName)
        {
            DBHelp Dbh = new DBHelp();
            Dbh.AddParameter("@username", UserName);
            string cmd = "select * from tbl_user where username = @username";
            SqlDataReader SqlReader = Dbh.ExecuteReader(cmd, CommandType.Text);
            if (SqlReader.Read())
            {
                return SqlReader.GetInt32(0);
            }
            else
            {
                return -1;
            }
        }

        /// <summary>
        /// Check UserName and Pass
        /// </summary>
        /// <param name="UserName"></param>
        /// <param name="Password"></param>
        /// <returns></returns>
        public static int GetUserID(String UserName, String Password)
        {
            DBHelp Dbh = new DBHelp();
            Dbh.AddParameter("@username", UserName);
            Dbh.AddParameter("@password", Password);
            string cmd = "select * from tbl_user where username=@username and password = @password";
            SqlDataReader SqlReader = Dbh.ExecuteReader(cmd, CommandType.Text);
            if (SqlReader.Read())
            {
                return SqlReader.GetInt32(0);
            }
            else
            {
                return -1;
            }
        }

        /// <summary>
        /// Check Email is Exist
        /// </summary>
        /// <param name="EmailAddress"></param>
        /// <returns></returns>
        public static int GetUserbyEmail(String EmailAddress)
        {
            DBHelp Dbh = new DBHelp();
            Dbh.AddParameter("@email", EmailAddress);
            string cmd = "select * from tbl_user where email = @email";
            SqlDataReader SqlReader = Dbh.ExecuteReader(cmd, CommandType.Text);
            if (SqlReader.Read())
            {
                return SqlReader.GetInt32(0);
            }
            else
            {
                return -1;
            }
        }

        public static int Insert(User user)
        {
            DBHelp Dbh = new DBHelp();
            Dbh.AddParameter("@username", user.UserName);
            Dbh.AddParameter("@password", user.Password);
            Dbh.AddParameter("@name_first", user.FirstName);
            Dbh.AddParameter("@name_last", user.LastName);
            Dbh.AddParameter("@email", user.Email);
            Dbh.AddParameter("@avatar", user.Avatar);
            Dbh.AddParameter("@active", user.Active);
            Dbh.AddParameter("@online", user.Online);
            Dbh.AddParameter("@created_date", user.CreateDate);

            SqlParameter UserID = new SqlParameter();
            UserID.DbType = DbType.Int32;
            UserID.Direction = ParameterDirection.Output;
            UserID.ParameterName = @"@user_id";
            Dbh.AddParameter(UserID);
            try
            {
                string cmd = "User_Insert";
                Dbh.ExecuteNonQuery(cmd, CommandType.StoredProcedure);
                return (int)UserID.Value;
            }
            catch (System.Exception)
            {
                return -1;
            }
            
        }

        public static void Update(User user)
        {
            DBHelp Dbh = new DBHelp();
            Dbh.AddParameter("@user_id", user.UserID);
            Dbh.AddParameter("@username", user.UserName);
            Dbh.AddParameter("@password", user.Password);
            Dbh.AddParameter("@name_first", user.FirstName);
            Dbh.AddParameter("@name_last", user.LastName);
            Dbh.AddParameter("@email", user.Email);
            Dbh.AddParameter("@avatar", user.Avatar);
            Dbh.AddParameter("@active", user.Active);
            Dbh.AddParameter("@online", user.Online);
            Dbh.AddParameter("@created_date", user.CreateDate);

            string cmd = "User_Update";
            Dbh.ExecuteNonQuery(cmd, CommandType.StoredProcedure);
        }

        public static void Delete(int UserID)
        {
            DBHelp Dbh = new DBHelp();
            Dbh.AddParameter("@user_id", UserID);
            string cmd = "User_Delete";
            Dbh.ExecuteNonQuery(cmd, CommandType.StoredProcedure);
        }
    }
}
